<!--
 * @Author: panda
 * @Date: 2022-05-18 09:39
 * @Description: 轮播图组件 TODO
-->
<template>
  <div>
    <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item v-for="item in videos" :key="item.id">
        <el-image
          :src="item.cover"
          alt=""
          fit="contain"
          @click="toDetail(item.id)"
        />
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script lang="ts">
import { videoApi, Video } from "@/api/video";
import { reactive, toRefs } from "vue-demi";
import { useRouter } from "vue-router";

interface State {
  videos: Array<Video>;
}

export default {
  name: "MainCarousel",
  setup() {
    const router = useRouter();
    const carousel = reactive<State>({
      videos: [],
    });

    videoApi
      .getTopVideos({})
      .then((res) => {
        carousel.videos = res.data.data;
      })
      .catch((error) => {
        console.log(error);
      });

    const toDetail = (id: number) => {
      const routeData = router.resolve({
        path: "/video/" + id,
      });
      window.open(routeData.href, "_blank");
    };

    return {
      ...toRefs(carousel),
      toDetail,
    };
  },
};
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
